<template>
  <div class="app-container">
    <el-card
      class="filter-container"
      shadow="never"
    >
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <el-button
          style="float:right"
          type="primary"
          @click="handleSearchList()"
          size="small"
        >
          查询搜索
        </el-button>
        <el-button
          style="float:right;margin-right: 15px"
          @click="handleResetSearch()"
          size="small"
        >
          重置
        </el-button>
      </div>
      <div style="margin-top: 15px">
        <el-form
          :inline="true"
          :model="listQuery"
          size="small"
          label-width="140px"
        >
          <el-form-item label="属性名称：">
            <el-input
              v-model="listQuery.prptyName"
              class="input-width"
              placeholder="输入名称"
              clearable
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="operate-container"
      shadow="never"
    >
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <el-button
        size="mini"
        class="btn-add"
        @click="handleAdd()"
      >添加</el-button>
    </el-card>
    <div class="table-container">
      <el-table
        ref="homeAdvertiseTable"
        :data="list"
        style="width: 100%;"
        @selection-change="handleSelectionChange"
        v-loading="listLoading"
        border
      >
        <el-table-column
          label="属性名称"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.prptyName }}</template>
        </el-table-column>
        <el-table-column
          label="展示名称"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.prptyNameShow }}</template>
        </el-table-column>
        <el-table-column
          label="类型"
          align="center"
        >
          <template slot-scope="scope">{{ prptyType[scope.row.prptyType] }}</template>

          <!-- <template slot-scope="scope">{{ scope.row.prptyType==='1'? '销售属性' : '自然属性'}}</template> -->
        </el-table-column>
        <el-table-column
          label="属性值"
          align="center"
        >
          <template slot-scope="scope">{{ scope.row.prptyValue }}</template>
          <!--          <template>{{ this.list1[this.prptyCode] }}</template>-->
        </el-table-column>
        <!-- <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleUpdate(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column> -->
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :page-size="listQuery.pageSize"
        :page-sizes="[5,10,15]"
        :current-page.sync="listQuery.pageNum"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import {
  curCourseBaseList,
  curCourseTypeList,
  curCourseBaseDelById,
  umsDeptList
} from '@/api/course'
import { prptyList, deletePrpty } from '@/api/goodsMs'
import { formatDate } from '@/utils/date'
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  prptyName: ''
}
export default {
  name: 'CourseList',
  data () {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      typeOptions: Object.assign({}),
      list: null,
      list1: [],
      total: null,
      prptyCode: '',
      prptyValue: '',
      listLoading: false,
      multipleSelection: [],
      operates: [
        {
          label: '删除',
          value: 0
        }
      ],
      operateType: null,
      courseTypeOptions: [],
      loading: false,
      deptOptions: [],
      prptyType: {
        1: '销售属性',
        2: '自然属性'
      }
    }
  },
  created () {
    this.getCourseType()
    this.getList()
  },
  filters: {
    formatDateTime (time) {
      if (time == null || time === '') {
        return 'N/A'
      }
      const date = new Date(time)
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    handleResetSearch () {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    handleSearchList () {
      this.listQuery.pageNum = 1
      this.getList()
      // console.log(this.listQuery)
    },
    handleSelectionChange (val) {
      this.multipleSelection = val
    },
    handleSizeChange (val) {
      this.listQuery.pageNum = 1
      this.listQuery.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.listQuery.pageNum = val
      this.getList()
    },
    handleDelete (index, row) {
      this.deleteCouerse(row.id)
    },
    handleVideoList (index, row) {
      this.$router.push({
        path: '/course-ms/videoList',
        query: { id: row.id }
      })
    },
    handleReview (index, row) {
      this.$router.push({ path: '/serv-ms/reviewServ', query: { id: row.id } })
    },
    handleAdd () {
      this.$router.push({ path: '/goods-ms/addGoodsList' })
    },
    handleUpdate (index, row) {
      this.$router.push({
        path: '/goods-ms/updateGoodsList',
        query: { id: row.id, prptyCode: row.prptyCode }
      })
    },
    getList () {
      prptyList(this.listQuery).then((response) => {
        console.log(response.data)
        this.list = response.data.list
        this.total = response.data.total
        const data = this.list.map((item, index) => {
          this.list1 = item.prptyValList
          this.prptyCode = item.prptyCode
          // console.log(this.list1)
          // console.log(this.list1.length)
          const data1 = this.list1.map((item, index) => {
            this.prptyValue += item.prptyValue
            if (this.list1.length - 1 !== index) {
              this.prptyValue += ';'
            }

            // console.log(index)
          })
          item.prptyValue = this.prptyValue
          this.prptyValue = ''
          // console.log(this.list1, this.list)
        })
      })
    },
    deleteCouerse (ids) {
      this.$confirm('是否要删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const params = new URLSearchParams()
        params.append('prptyIds', ids)
        // console.log(ids)
        deletePrpty({ prptyIds: ids }).then((response) => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      })
    },
    getCourseType () {
      this.courseTypeOptions = []
      // 获取课程类型枚举
      curCourseTypeList().then((response) => {
        this.courseTypeOptions = response.data.list
      })
    }
    // remoteMethod (query) {
    //   if (query !== '') {
    //     this.loading = true
    //     umsDeptList({ pageSize: 50, pageNum: 1 }).then(response => {
    //       this.loading = false
    //       this.deptOptions = response.data.list.map(item => {
    //         return { value: `${item.id}`, label: `${item.deptName}` }
    //       })
    //     })
    //   } else {
    //     this.deptOptions = []
    //   }
    // }
  }
}
</script>
<style scoped>
.input-width {
  width: 203px;
}
</style>
